package com.offcn.service;

import com.offcn.dao.CourseDao;
import com.offcn.dao.CourseDaoImpl;
import com.offcn.entity.Course;
import com.offcn.utils.PageUtils;

import java.io.File;
import java.util.List;

public class CourseServiceImpl implements CourseService {
    private CourseDao courseDao = new CourseDaoImpl();

    /**
     * 添加课程
     * @param course
     * @return
     */
    @Override
    public boolean add(Course course) {
        return courseDao.add(course)>0;
    }

    /**
     * 分页查询
     * @param search
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageUtils find(String search, int  currentPage, int  pageSize) {
        StringBuffer stringBuffer = new StringBuffer("select * from course where courseName like ? ");
        int size = courseDao.find(stringBuffer.toString(), "%" + search + "%").size();
        stringBuffer.append(" limit ?,?");
        List<Course> courseList = courseDao.find(stringBuffer.toString(), "%" + search + "%", (currentPage - 1) * pageSize, pageSize);
        PageUtils pageUtils = new PageUtils(pageSize,currentPage,size,courseList);
        return pageUtils;
    }

    /**
     * 修改课程
     * @param course
     * @return
     */
    @Override
    public boolean update(Course course) {
        return courseDao.updateCourse(course)>0;
    }

    /**
     * 删除数据
     * @param cids
     * @param
     */
    @Override
    public boolean del(String[] cids) {
        try {
            for (String cid : cids) {
                courseDao.del(cid);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}
